package com.spc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spc.dao.FoodDao;
import com.spc.dao.IllnessDao;
import com.spc.entity.PageResult;
import com.spc.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {


    @Autowired
    private FoodDao foodDao;

    @Autowired
    private IllnessDao illnessDao;

    @Override
    public List<Map> findAllIllness() {

        List<Map> list =  foodDao.findAllIllness();

        return list;
    }

    @Override
    public List<Map> findAllType() {

        List<Map> list =  foodDao.findAllType();

        return list;
    }

    /**
     * 添加疾病库数据
     * @param map
     */
    @Override
    public void add(Map map) {


        foodDao.add(map);



        if (map!=null && map.size()>0){



            List<Map> illnessList = (List<Map>) map.get("illnessList");

            if (illnessList!=null && illnessList.size()>0){

                Integer foodid = (Integer) map.get("foodid");

                //判断是否有关系表
                List<Integer> foodIllness = foodDao.findFoodIllness(foodid);

                if (foodIllness!=null && foodIllness.size()>0){
                    //先删除再添加
                    foodDao.deleteFoodIllness(foodid);

                    List<Map> maps = new ArrayList<>();

                    Map map2 = null;
                    for (Map map1 : illnessList) {

                        if (map1!=null && map1.size()>0){

                            String value = (String) map1.get("value");

                            //为1  用户选着是
                            if (value!= null && value.equals("1")){

                                map2 = new HashMap<>();

                                Integer id = (Integer) map1.get("id");

                                map2.put("foodid",foodid);
                                map2.put("id",id);
                                maps.add(map2);

                            }

                        }

                    }

                            foodDao.addFoodIllness(maps);


                }else {

                    //直接添加
                    List<Map> maps = new ArrayList<>();

                    Map map2 = null;
                    for (Map map1 : illnessList) {

                        if (map1!=null && map1.size()>0){

                            String value = (String) map1.get("value");

                            //为1  用户选着是
                            if (value!= null && value.equals("1")){

                                map2 = new HashMap<>();

                                Integer id = (Integer) map1.get("id");

                                map2.put("foodid",foodid);
                                map2.put("id",id);
                                maps.add(map2);

                            }

                        }

                    }

                    foodDao.addFoodIllness(maps);


                }








            }



        }






    }

    /**
     * 查询疾病库分页数据
     * @param map
     * @return
     */
    @Override
    public PageResult findPage(Map map) {


        Integer currentPage = (Integer) map.get("currentPage");//当前页码
        Integer pageSize = (Integer) map.get("pageSize");//每页记录数
        String queryString = (String) map.get("queryString");//查询条件
        //调用dao完成分页查询，使用mybatis分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        Page<Map<Object,Object>> page = foodDao.findByCondition(queryString);

        //使用分页助手中的方法获取总记录数和分页数据
        long total = page.getTotal();
        List<Map<Object,Object>> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);


        return pageResult;
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {

        Map map =  foodDao.findById(id);

        //查询关系表
       List<Integer> list = foodDao.findFoodIllness(id);
        List<Map> list1 = new ArrayList<>();
       if (list!=null && list.size()>0){


           for (Integer integer : list) {

               Map byId = illnessDao.findById(integer);



               byId.put("value","1");

               list1.add(byId);


           }
           map.put("illnessList",list1);
       }



        return map;
    }

    /**
     * 编辑更改数据
     * @param map
     * @param id
     */
    @Override
    public void edit(Map map, Integer id) {

        //根据id查询关联表是否为空
        List<Integer> foodIllness = foodDao.findFoodIllness(id);

        if (foodIllness!=null && foodIllness.size()>0){
            //根据id删除 关系数据  再添加

            foodDao.deleteFoodIllness(id);

            List<Map> maps = new ArrayList<>();
            Map map2 =null;

            List<Map> illnessList = (List<Map>) map.get("illnessList");

            if (illnessList!=null && illnessList.size()>0){




                for (Map map1 : illnessList) {

                    if (map1!=null && map1.size()>0){

                        String value = (String) map1.get("value");

                        //为1  用户选着是
                        if (value!= null && value.equals("1")){
                            map2 = new HashMap();
                            Integer id1 = (Integer) map1.get("id");
                            map2.put("foodid",id);
                            map2.put("id",id1);
                            maps.add(map2);
                        }

                    }

                }
                if (maps!=null){

                    foodDao.addFoodIllness(maps);
                }




            }


        }else {
            //执行 添加操作
            List<Map> maps = new ArrayList<>();
            Map map2 =null;

            List<Map> illnessList = (List<Map>) map.get("illnessList");

            if (illnessList!=null && illnessList.size()>0){


                for (Map map1 : illnessList) {

                    if (map1!=null && map1.size()>0){

                        String value = (String) map1.get("value");

                        //为1  用户选着是
                        if (value!= null && value.equals("1")){

                            map2 = new HashMap();
                            Integer id1 = (Integer) map1.get("id");
                            map2.put("foodid",id);
                            map2.put("id",id1);
                            maps.add(map2);
                        }

                    }

                }
                foodDao.addFoodIllness(maps);


            }


        }

        if (map!=null && map.size()>0){




            map.put("foodid",id);

            foodDao.edit(map);

        }


    }

    @Override
    public void deleteById(Integer id) {




        foodDao.deleteById(id);

        foodDao.deleteFoodIllness(id);

    }

    /**
     * 添加科室
     * @param map
     */
    @Override
    public void addDepartments(Map map) {

        if (map!=null && map.size()>0){

            String name = (String) map.get("type");

            Integer count = foodDao.findByName(name);


            if (count>0){

                //执行update操作
                foodDao.updateDepartments(map);

            }else {

                //执行添加操作
                foodDao.addDepartments(map);

            }


        }









    }

    /**
     * 回显操作
     * @return
     */
    @Override
    public List<Map> findAllDepartments() {

        List<Map> list = foodDao.findAllDepartments();


        return list;
    }




}
